﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Tradex.Connectivity {

    /// <summary>
    /// This class encapsules the parameters to be used for the ParameterHost.
    /// </summary>
    public sealed class ConnectorHostParameters {

        private ConnectorHostParameters() {
        }

        /// <summary>
        /// The string key to be used for the connector.
        /// </summary>
        public string Key { get; internal set; }

        /// <summary>
        /// The name of the connector.
        /// </summary>
        public string Connector { get; internal set; }

        /// <summary>
        /// Indicates whether the interface should be shown. The default is "false"
        /// </summary>
        public bool Interface { get; internal set; }

        /// <summary>
        /// Indicates whether the ConnectorName should run in manual mode. The default is "false"
        /// </summary>
        public bool Manual { get; internal set; }

        /// <summary>
        /// Gets the preloaded connection strin. This is not really useful in automatic mode, but nice for
        /// preinitializing a user interface with.
        /// </summary>
        public string Connection { get; internal set; }

        /// <summary>
        /// Parses a connector parameter set from the arguments given in the command line.
        /// </summary>
        /// <param name="args"></param>
        /// <returns></returns>
        internal static ConnectorHostParameters Parse(string[] args) {
            ConnectorHostParameters parameters = new ConnectorHostParameters ();
            int paramPos = 0;
            for (int argPos = 0; argPos < args.Length; argPos++) {
                string arg = args[argPos];

                if (arg.StartsWith ("/")) {
                    string param = null;
                    int separatorIndex = arg.IndexOf(':');
                    if (separatorIndex != -1) {
                        string key = arg.Substring(0, separatorIndex);
                        param = arg.Substring(separatorIndex + 1);
                        arg = key;
                    }
                    switch (arg) {
                        case "/Interface":
                            parameters.Interface = true;
                            break;
                        case "/Manual":
                            parameters.Manual = true;
                            break;
                        case "/Connection":
                            parameters.Connection = param;
                            break;
                        default:
                            throw new ArgumentException("Invalid Parameters", "args");
                    }
                    continue;
                }
                switch (paramPos) {
                    case 0:
                        parameters.Connector = arg;
                        break;
                    case 1:
                        parameters.Key = arg;
                        break;
                    case 2:
                        throw new ArgumentException("Invalid Parameters", "args");
                }
                paramPos++;

            }
            return parameters;
        }

    }

}
